package redis

import "strconv"

const Shop_stock = "ShopStock_id_"

// 像虚拟库存中添加商品
func SyncShopStock(GoodId, GoodStock int) error {
	itoa := strconv.Itoa(GoodId)
	s := Shop_stock + itoa
	for i := 0; i < GoodStock; i++ {
		err := Client.RPush(s, GoodId).Err()
		if err != nil {
			return err
		}
	}
	return nil
}

// 查询库存剩余量
func LengthShopStock(GoodId int) (int64, error) {
	itoa := strconv.Itoa(GoodId)
	s := Shop_stock + itoa
	result, err := Client.LLen(s).Result()
	return result, err
}

// 扣减虚拟库存
func DelShopStock(goodId, goodStock int) error {
	itoa := strconv.Itoa(goodId)
	s := Shop_stock + itoa
	for i := 0; i < goodStock; i++ {
		err := Client.LPop(s).Err()
		if err != nil {
			return err
		}
	}

	return nil
}
